package com.example.travel.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.ViewFlipper;


import androidx.fragment.app.Fragment;

import com.example.travel.Interface.Implements.UserInterfaceImpl;
import com.example.travel.R;

import java.util.ArrayList;
import java.util.HashMap;


public class MyContactFragment extends Fragment {
    ListView lv;
    ArrayList<HashMap<String, Object>> userList=new ArrayList<HashMap<String, Object>>();
    SimpleAdapter sa;
    UserInterfaceImpl userInterface;
    int positoin;
    int user_id;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.travel_list, container, false);
        ViewFlipper viewFlipper=(ViewFlipper)view.findViewById(R.id.vf);
        viewFlipper.addView(addImgView(R.drawable.image4));
        viewFlipper.addView(addImgView(R.drawable.image5)) ;
        viewFlipper.addView(addImgView(R.drawable.image6));
        viewFlipper.setInAnimation(getContext(),android.R.anim.slide_in_left);
        viewFlipper.setOutAnimation(getContext(),android.R.anim.slide_out_right);
        viewFlipper.setFlipInterval(3000);
        viewFlipper.startFlipping();

        lv=view.findViewById(R.id.listView);
        //通过业务逻辑层从表user_table把所有联系人取出来
        userInterface=new UserInterfaceImpl(getContext());
        userList=userInterface.getAllUsers();
        sa=new SimpleAdapter(getContext(),userList,R.layout.browselist,
                new String[]{"user_name","phone"},new int[]{R.id.tv_name,R.id.tv_phone});
        lv.setAdapter(sa);

        //注册上下文菜单
        registerForContextMenu(lv);


        return view;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
       // menu.add("拨号");
        //menu.add("发短信");
        menu.add("修改商品");
        menu.add("删除商品");
        super.onCreateContextMenu(menu, v, menuInfo);
    }
/*
    @Override
   public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo infor=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        positoin=infor.position;
        user_id=((Integer) userList.get(positoin).get("user_id")).intValue();
        String phone=(String)userList.get(positoin).get("phone");
        if(item.getTitle().equals("拨号")){
            UtilPermission.checkCall_Phone_Permission(getActivity());
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_CALL);
            Uri uri=Uri.parse("tel:"+phone);
            intent.setData(uri);
            startActivity(intent);

        }else if(item.getTitle().equals("发短信")){
            UtilPermission.checkSend_SMS_Permission(getActivity());
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_SENDTO);
            Uri uri=Uri.parse("smsto:"+phone);
            intent.setData(uri);
            startActivity(intent);

        }else
         if(item.getTitle().equals("修改商品")){
            //调用UpdateContactFragment
            Fragment updateContactFragment =new UpdateContactFragment();
            Bundle bundle=new Bundle();
            bundle.putInt("user_id",user_id);
            updateContactFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.flconent,updateContactFragment)
                    .commit();

        }else if(item.getTitle().equals("删除商品")){
            AlertDialog ad=createDialog();
            ad.show();

        }
        return super.onContextItemSelected(item);
    }
    */
     @Override
     public boolean onContextItemSelected(MenuItem item){
       AdapterView.AdapterContextMenuInfo infor=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
         user_id=((Integer) userList.get(positoin).get("user_id")).intValue();
         if(item.getTitle().equals("删除商品")){
           AlertDialog ad=createDialog();
           ad.show();

          }else
         if(item.getTitle().equals("修改商品")){
             //调用UpdateContactFragment
             Fragment updateContactFragment =new UpdateContactFragment();
             Bundle bundle=new Bundle();
             bundle.putInt("user_id",user_id);
             updateContactFragment.setArguments(bundle);
             getFragmentManager().beginTransaction().replace(R.id.flconent,updateContactFragment)
                     .commit();
         }
             return super.onContextItemSelected(item);
     }


    AlertDialog createDialog(){
        AlertDialog dialog;
        AlertDialog.Builder ad=new AlertDialog.Builder(getContext());
        ad.setTitle("删除商品对话框");
        ad.setIcon(R.mipmap.ic_launcher_round);
        ad.setMessage("确定删除吗!");
        ad.setPositiveButton("确定",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteUser();
                getFragmentManager().beginTransaction().replace(R.id.flconent,new MyContactFragment())
                        .commit();
            }
        });
        ad.setNegativeButton("取消",null);
        dialog=ad.create();
        return dialog;
    }
    public void deleteUser(){
        userInterface=new UserInterfaceImpl(getContext());
        userInterface.deleteUserById(user_id);
    }


    public View addImgView(int id) {
        ImageView iv=new ImageView(getContext());
        iv.setBackgroundResource(id);
        return iv;
    }
}
